<?php

/**
 * Chat main controller
 *
 * @author     Mon Zafra <monzee at gmail>
 * @copyright  (c)2009 Mon Zafra
 * @category   Mz
 * @package    modules
 * @subpackage chat
 * @license    http://mz-project.googlecode.com/svn/trunk/LICENSE    MIT License
 * @version    SVN: $Id: IndexController.php 9 2009-02-22 07:24:16Z monzee $
 */
class Chat_IndexController extends Zend_Controller_Action
{

    public function indexAction()
    {
        $form = new Chat_EntryForm(array(
            'action' => $this->_helper->url('post')
        ));

        $this->view->messages = $this->_helper->module('chat')->getLast(10, true);
        $this->view->form = $form;
    }

    public function postAction()
    {
        $req = $this->getRequest();

        if ($req->isPost()) {
            $form = new Chat_EntryForm();
            $values = $req->getPost();

            if (!$form->isValid($values)) {
                $this->_setParam('chatEntryForm', $form);
                $this->_forward('index', 'index', 'default');
                return;
            } else {
                $chat = new Chat_Chatbox();
                $chat->addMessage($form->getValues());
                // TODO: set cookie
                $this->_redirect($req->getPost('_redirect', '/'));
                return;
            }

        }
        $this->_forward('index');
    }

}
